<template>
  <a-modal
    title="业态管理"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

<!--        <a-form-item-->
<!--          label="业态菜单"-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--        >-->
<!--          <a-select placeholder="业态菜单" :disabled="isDisabled" :allowClear="true"-->
<!--                    v-decorator="[ 'pcode', {rules: []} ]">-->
<!--            <a-select-option v-for="item in parentMenus" :key="item.cateCode">{{item.cateName}}</a-select-option>-->
<!--          </a-select>-->
<!--        </a-form-item>-->

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="业态名称"
        >
          <a-input
            placeholder="业态名称"
            v-decorator="[ 'brandName', {rules: [{ required: true, message: '请输入。。。' }]}]"
          ></a-input>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import { saveOrUpdate } from '@/api/system/category'

  export default {
    data () {
      return {
        id: null,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        isDisabled: false,
        parentMenus: [],

        form: this.$form.createForm(this)
      }
    },

    methods: {
      add () {
        this.visible = true
      },
      edit (record) {
        this.id = record.id
        this.visible = true
        this.isDisabled = true
        const { form: { setFieldsValue } } = this
        this.$nextTick(() => {
          const formData = pick(record, ['brandName'])
          setFieldsValue(formData)
        })
      },
      handleSubmit () {
        const _this = this
        const { form: { validateFields } } = this
        _this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            values.id = _this.id
            saveOrUpdate(values).then((res) => {
              _this.confirmLoading = false
              if (res.code === 200) {
                _this.handleCancel()
                _this.$emit('ok')
              }
            })
          } else {
            _this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.visible = false
        this.isDisabled = false
        this.form.resetFields()
      }
    }
  }
</script>
